/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package contextManagement;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 *
 * @author huangqing
 */
public class ContextBroker implements PropertyChangeListener{
    private static ContextBroker theBroker;
    private PropertyChangeSupport myPCS = new PropertyChangeSupport(this);
    private ContextProvider tempsprovider;
    private ContextProvider uprefprovider;
    private TimeGoesOn horloge;


	private String temp = "0.0";
	private String uPref;



    private ContextBroker(){
        tempsprovider = new TemperatureProvider();
        uprefprovider = new UserPreferenceProvider();
        myPCS = new PropertyChangeSupport(this);
        addPropertyChangeListener("deltat",this);
	addPropertyChangeListener("chItem",this);
	horloge = new TimeGoesOn(this);
    }


   public final synchronized static ContextBroker getInstance(){
	if (theBroker == null){
		theBroker = new ContextBroker();
	} else {
		theBroker.tempsprovider = new TemperatureProvider();
		theBroker.uprefprovider = new UserPreferenceProvider();
		theBroker.temp = "0.0";
        }
	return theBroker;
    }


    public void updateProvidersValues(){
	String oldTemp = temp;
	temp = tempsprovider.getValue();
	String oldUPref = uPref;
	uPref = uprefprovider.getValue();

	firePropertyChange(new PropertyChangeEvent(this, "temps", oldTemp, temp) );
	firePropertyChange(new PropertyChangeEvent(this, "uPref", oldUPref, uPref));
    }

    public void addPropertyChangeListener(String property, PropertyChangeListener pcl) {
        myPCS.addPropertyChangeListener(property, pcl);
    }

    public void firePropertyChange(PropertyChangeEvent pce){
        myPCS.firePropertyChange(pce);
    }


    
    public void propertyChange(PropertyChangeEvent pce) {
         String property = pce.getPropertyName();
         if (property.equals("deltat") || property.equals("chItem")){
             updateProvidersValues();
         }
    }

    public void changeUPref(String uPref){
	uprefprovider.setValue(uPref);
    }

   


}
